import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

const BasicExample = (history) => (

  <Router>
    {console.log(console.log('BasicExample', history))}
    <div>
      <ul>
        <li>
          <Link to={`${history.match.url}/Home`}>二级路由Home</Link>
        </li>
        <li>
          <Link to={`${history.match.url}/about`} >二级路由About</Link>
        </li>
        <li>
          <Link to={`${history.match.url}/topics`}>二级路由Topics</Link>
        </li>
      </ul>
      <hr />
      <Route exact path={`${history.match.url}/Home`} component={Home} />
      <Route path={`${history.match.url}/about`} component={About} />
      <Route path={`${history.match.url}/topics`} component={Topics} />
    </div>
  </Router>
);

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
);

const About = () => (
  <div>
    <h2>About</h2>
  </div>
);

const Topics = (history) => {
  const { match } = history
  return <div>
    <h2>Topics</h2>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>Rendering with React</Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>Components</Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>Props v. State</Link>
      </li>
    </ul>

    <Route path={`${match.url}/:topicId`} component={Topic} />
    <Route
      exact
      path={match.url}
      render={() => <h3>Please select a topic.</h3>}
    />
  </div>
}

const Topic = ({ match }) => (
  <div>
    <h3>{match.params.topicId}</h3>
  </div>
);

export default BasicExample;